package news.controller;

import news.service.IQueryUserService;
import news.service.QueryUserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.Response;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @author Administrator
 * 添加主题
 */
@WebServlet("/signIn")
public class SignInController extends HttpServlet {
    private IQueryUserService iQueryUserService = new QueryUserServiceImpl();
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应内容类型
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String check_pwd = iQueryUserService.findByName(username).get(0).getPassword();
        PrintWriter out = response.getWriter();
        String res = "";
        if (password.equals(check_pwd)) {
            // 如果不存在 session 会话，则创建一个 session 对象
            HttpSession session = request.getSession(true);
                session.setAttribute("username", username);
            request.getRequestDispatcher(request.getContextPath()+"/news/main/main-welcome.jsp").forward(request,response);
        }else {
            out.print("<script>alert('密码错误');window.location.href='/news/user/welcome-log.jsp';</script>");
//
        }
    }
}